package com.ibiaoke.boss.controller;

import com.ibiaoke.common.base.BaseController;
import com.ibiaoke.common.base.Page;
import com.ibiaoke.common.consts.CommonConsts;
import com.ibiaoke.common.consts.WeChatConsts;
import com.ibiaoke.common.response.Res;
import com.ibiaoke.common.utils.AssertUtils;
import com.ibiaoke.customer.dao.XcxOpenGroupDao;
import com.ibiaoke.customer.entity.Customer;
import com.ibiaoke.customer.entity.ThirdPartLogin;
import com.ibiaoke.customer.service.ThirdPartLoginService;
import com.ibiaoke.customer.entity.XcxOpenGroup;
import com.ibiaoke.goods.entity.XcxGoods;
import com.ibiaoke.goods.service.XcxGoodsService;
import com.ibiaoke.order.consts.XcxOrderStatusConst;
import com.ibiaoke.order.dao.XcxOrderDao;
import com.ibiaoke.order.entity.XcxOrder;
import com.ibiaoke.order.entity.so.SubmitOrderSO;
import com.ibiaoke.order.entity.vo.XcxOrderVO;
import com.ibiaoke.order.service.XcxOpenGroupService;
import com.ibiaoke.order.service.XcxOrderService;
import com.ibiaoke.system.dao.ShopDao;
import com.ibiaoke.system.entity.Shop;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * @author Healer
 * @date 2018/3/6/16:28;
 * 小程序的订单
 */
@Controller
public class XcxOrderController extends BaseController{

    @Autowired
    private XcxOrderService xcxOrderService;

    @Autowired
    private ThirdPartLoginService thirdPartLoginService;

    @Autowired
    private XcxOpenGroupService xcxOpenGroupService;

    @Autowired
    private XcxGoodsService xcxGoodsService;

    @Autowired
    private XcxOrderDao xcxOrderDao;

    @Autowired
    private ShopDao shopDao;

    @Autowired
    private XcxOpenGroupDao xcxOpenGroupDao;

    /**
     * 开团
     */
    @RequestMapping("/XcxOpenGroup")
    @ResponseBody
    public Res submitPtGoodsOrder(Integer XcxGoodsId,Integer addressId,Integer deliveryType,Integer shopId,String remark,
                                  Integer expressPrice,Integer paymentType){
        Customer customer = getLoginUser();
        AssertUtils.notNull(customer, "请登录");
        String openId = (String) getSession().getAttribute(WeChatConsts.Xcx_Id);
        ThirdPartLogin thirdPartLogin = thirdPartLoginService.getByXid(openId);
        XcxOrder xcxOrder = xcxOrderService.submitXcxGoodsOrder(customer,XcxGoodsId,addressId,deliveryType,thirdPartLogin,
                                                                shopId,remark,expressPrice,paymentType);
        return Res.create().addResponse("xcxOrder",xcxOrder);
    }

    /**
     * 参团
     */
    @RequestMapping("/JoinXcxOpenGroup")
    @ResponseBody
    public Res joinXcxOpenGroup(Integer groupId, Integer deliveryType,Integer addressId,Integer shopId,String remark,
                                Integer expressPrice,Integer paymentType){
        Customer customer = getLoginUser();
        AssertUtils.notNull(customer, "请登录");
        String openId = (String) getSession().getAttribute(WeChatConsts.Xcx_Id);
        ThirdPartLogin thirdPartLogin = thirdPartLoginService.getByXid(openId);
        XcxOpenGroup xcxOpenGroup = xcxOpenGroupService.getOpenGroup(groupId);
        XcxOrder xcxOrder = xcxOrderService.joinXcxOpenGroup(xcxOpenGroup,customer,thirdPartLogin,deliveryType,addressId,
                                                             shopId,remark,expressPrice,paymentType);
        return Res.create().addResponse("xcxOrder",xcxOrder);
    }

    /**
     * 商品直接下单
     */
    @ResponseBody
    @RequestMapping("/XcxGoodsOrder")
    public Res submitGoodsOrder(@Valid SubmitOrderSO submitOrderSO, BindingResult result) {
        //handleValidate(result);
        Customer customer = getLoginUser();
        submitOrderSO.setUserId(customer.getCustomerId());
        XcxOrder order = xcxOrderService.XcxGoodsOrder(submitOrderSO);
        return new Res().addResponse("order", order);
    }

    /**
     * 获取当前商品开团人列表
     */
    @RequestMapping("/listXcxOpenGroup")
    @ResponseBody
    public Res listXcxOpenGroup(Integer ptGoodsId,Integer shopId,Page page){
        List<XcxOpenGroup> xcxOpenGroupList = xcxOpenGroupService.listXcxOpenGroup(ptGoodsId,shopId,page);
        return Res.create().addResponse("xcxOpenGroupList",xcxOpenGroupList).addResponse("page",page);
    }

    /**
     * 去参团，获取当前团详细
     */
    @RequestMapping("/xcxOpenGroupList")
    @ResponseBody
    public Res xcxOpenGroup(Integer xcxGoodsId, String OpenCode){
        Customer customer = getLoginUser();
        Integer IsJoin=0;
        List<XcxOpenGroup> xcxOpenGroupList = xcxOpenGroupService.checkXcxOpenGroup(OpenCode,xcxGoodsId,customer.getCustomerId());
        if(xcxOpenGroupList != null && xcxOpenGroupList.size()>0){
            for(XcxOpenGroup thisXcxOpenGroup : xcxOpenGroupList){
                if(thisXcxOpenGroup.getIsPay() == 1 ){
                    IsJoin = 1;
                }
            }
        }
        List<XcxOrder> xcxOrder = xcxOrderDao.getXcxOrderList(OpenCode);
        XcxGoods xcxGoods = xcxGoodsService.getXcxGoodsByGoodsId(xcxGoodsId,xcxOrder.get(0).getShopId());
        List<XcxOpenGroup> openGroupList = xcxOpenGroupService.getPtOpenGroupByOpenCodeAndPtGoodsId(OpenCode, xcxGoodsId);
        Shop shop = shopDao.selectByPrimaryKey(xcxOrder.get(0).getShopId());
        return Res.create()
                .addResponse("isJoin",IsJoin)
                .addResponse("shopId",xcxOrder.get(0).getShopId())
                .addResponse("shopName",shop.getShopName())
                .addResponse("xcxGoods",xcxGoods)
                .addResponse("openGroupList",openGroupList);
    }

    /**
     * 订单列表
     */
    @RequestMapping("/listXcxOrder")
    @ResponseBody
    public Res listXcxOrder(XcxOrderVO xcxOrder, Page page) {
        Customer customer = getLoginUser();
        xcxOrder.setUserId(customer.getCustomerId());
        List<XcxOrder> xcxOrderList = xcxOrderService.xcxOrderList(xcxOrder, page);
        return Res.create().addResponse("xcxOrderList",xcxOrderList).addResponse("page",page);
    }

    /**
     * 订单详情
     * @param orderId 订单ID
     * @return
     */
    @RequestMapping("/getOrderById")
    @ResponseBody
    public Res getOrderById(Integer orderId) {
        XcxOrder xcxOrder = xcxOrderService.getOrderById(orderId);
        Shop shop = shopDao.selectByPrimaryKey(xcxOrder.getShopId());
        return Res.create().addResponse("xcxOrder",xcxOrder).addResponse("shop",shop);
    }

    /**
     * 确认订单
     */
    @RequestMapping("/xcxOrderConfirm")
    @ResponseBody
    public Res xcxOrderConfirm(XcxOrder xcxOrder) {
        Customer customer = getLoginUser();
        xcxOrder.setReceivedRecord((byte) CommonConsts.YES);
        xcxOrder.setRecordTime(new Date());
        xcxOrderService.ConfirmOrder(customer,xcxOrder);
        return Res.create();
    }
    /**
     * 取消订单
     */
    @RequestMapping("/xcxCancelOrder")
    @ResponseBody
    public Res xcxCancelOrder(XcxOrder xcxOrder) {
        Customer customer = getLoginUser();
        xcxOrderService.cancelOrder(customer.getCustomerId(), xcxOrder.getOrderId());
        return Res.create();
    }

    /**
     * 删除订单
     */
    @RequestMapping("/delXcxOrder")
    @ResponseBody
    public Res delXcxOrder(Integer orderId) {
        Customer customer = getLoginUser();
        xcxOrderService.delXcxOrder(customer, orderId);
        return Res.create();
    }

    /**
     * 获取订单数量
     * @return
     */
    @RequestMapping("/getOrderCount")
    @ResponseBody
    public Res getOrderCount() {
        Customer customer = getLoginUser();
        Long NoPayCount = xcxOrderService.getOrderCountByStatus(customer.getCustomerId(), XcxOrderStatusConst.NO);
        Long PtCount = xcxOrderService.getOrderCountByStatus(customer.getCustomerId(), XcxOrderStatusConst.PT_ING);
        Long DeliveredCount = xcxOrderService.getOrderCountByStatus(customer.getCustomerId(), XcxOrderStatusConst.DELIVERED);
        Long ReceivedCount = xcxOrderService.getOrderCountByStatus(customer.getCustomerId(), XcxOrderStatusConst.RECEIVED);
        ThirdPartLogin thirdPartLogin = thirdPartLoginService.getInfo(customer.getCustomerId());
        AssertUtils.notNull(thirdPartLogin,"账户异常");
        return Res.create()
                .addResponse("Username",thirdPartLogin.getNickName())
                .addResponse("CustomerImg",thirdPartLogin.getHeadImageUrl())
                //待付款数量
                .addResponse("NoPaycount",NoPayCount)
                //拼团中数量
                .addResponse("PtCount",PtCount)
                //待收货数量
                .addResponse("DeliveredCount",DeliveredCount)
                //已完成数量
                .addResponse("ReceivedCount",ReceivedCount);
    }
}
